import java.util.Deque;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 一些排序方法实现
 * User: xwl
 * Date: 2023-05-26
 * Time: 22:18
 */
public class Sort {

    /**
     * 直接插入排序
     * @param array:待排序数组
     */
    public static void insertSort(int[] array) {
        int i = 1;
        while (i < array.length) {
            int tmp = array[i];
            int j = i - 1;
            while (j >= 0) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
                j--;
            }
            array[j + 1] = tmp;
            i++;
        }
    }

    /**
     * 希尔排序
     * @param array:待排序数组
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            shell(array,gap);
            gap = gap/2;
        }
        // 整体进行插入排序
        shell(array,1);
    }

    /**
     *
     * @param array
     * @param gap
     */
    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                }else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    /**
     * 选择排序
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    /**
     * 选择排序2
     * @param array
     */
    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = right;
            for (int i = left + 1; i < array.length; i++) {
                if (array[i] < minIndex) {
                    minIndex = i;
                }
                if (array[i] > maxIndex) {
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }

    }

    /**
     * 交换
     * @param array
     * @param i
     * @param j
     */
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /**
     * 堆排
     * @param array
     */
    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array, 0, end);
            shiftDown(array,0,end);
            end--;
        }
    }

    /**
     * 创建大根堆
     * @param array
     */
    public static void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(array,parent,array.length);
        }
    }

    /**
     * 向下调整
     * @param array
     * @param parent
     * @param len
     */
    public static void shiftDown(int[] array, int parent, int len) {
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    /**
     * 冒泡排序
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array,j,j+1);
                    flg = true;
                }
            }
            if (flg == false) {
                return;
            }
        }
    }

    /**
     * 快速排序
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    /**
     * 以基准pivot划分数组
     * @param array
     * @param start
     * @param end
     */
    public static void quick(int[] array, int start, int end) {
        if (start >= end) {
            return;
        }
        if (end - start + 1 <= 15) {
            // 插入排序
            return;
        }
        // 三数取中取基准
        int index = midThree(array, start, end);
        swap(array, index, start);
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    /**
     * 实现(挖孔法)
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    /**
     * 实现(交换法)hoare
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition2(int[] array, int left, int right) {
        int tmp = array[left];
        int i = left;
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    /**
     * 实现(双指针)
     * @param array
     * @param left
     * @param right
     * @return
     */
    public static int partition3(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while (cur <= right) {
            if (array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array, cur, prev);
            }
            cur++;
        }
        swap(array, prev, left);
        return prev;
    }

    /**
     * 三数取中法
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int midThree(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[mid] < array[left]) {
                return left;
            }else if (array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (array[mid] < array[right]) {
                return right;
            }else if (array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    /**
     * 直接插入排序
     * @param array:待排序数组
     */
    public static void insertSort2(int[] array, int left, int right) {
        int i = left + 1;
        while (i < array.length) {
            int tmp = array[i];
            int j = i - 1;
            while (j >= 0) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                }else {
                    break;
                }
                j--;
            }
            array[j + 1] = tmp;
            i++;
        }
    }

    /**
     * 非递归快速排序
     * @param array
     */
    public static void quickSort2(int[] array) {
        Deque<Integer> stack = new LinkedList<>();
        int left = 0;
        int right = array.length - 1;
        do {
            int pivot = partition(array, left, right);
            if (pivot > left + 1) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if (pivot < right - 1) {
                stack.push(pivot + 1);
                stack.push(right);
            }
            left = stack.pop();
            right = stack.pop();
        }while (!stack.isEmpty());
    }

    /**
     * 归并排序
     * @param array
     */
    public static void mergerSort(int[] array) {
        mergeSortFunc(array,0,array.length - 1);
    }

    /**
     * 归并排序(非递归)
     * @param array
     */
    public static void mergerSort2(int[] array) {
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += gap * 2) {
                int left = i;
                int mid = left + gap - 1;
                if (mid >= array.length) {
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if (right >= array.length) {
                    right = array.length - 1;
                }
                merge(array,left,right,mid);
            }
            gap *= 2;
        }
    }

    /**
     * 实现(递归)
     * @param array
     * @param left
     * @param right
     */
    public static void mergeSortFunc(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        //
        int mid = (left + right) / 2;
        mergeSortFunc(array, left, mid);
        mergeSortFunc(array, mid + 1, right);
        merge(array,left,right,mid);
    }

    /**
     * 合并
     * @param array
     * @param start
     * @param end
     * @param mid
     */
    public static void merge(int[] array, int start, int end, int mid) {
        int s1 = start;
        int s2 = mid + 1;
        int[] tmp = new int[end - start + 1];
        // tmp数组的下标
        int k = 0;
        while (s1 <= mid && s2 < end) {
            if (array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= mid) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= end) {
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            array[i+start] = tmp[i];
        }
    }

    /**
     * 计数排序
     * @param array
     */
    public static void countSort(int[] array) {
        int max = array[0];
        int min = array[0];
        // 找到数组中的最大值和最小值
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
            if (array[i] > max) {
                max = array[i];
            }
        }
        // 定义计数数组长度
        int len = max - min + 1;
        int[] count = new int[len];
        // 遍历数组，在计数数组中记录每个数组出现的次数
        for (int i = 0; i < array.length; i++) {
            count[array[i] - min]++;
        }
        // 用来遍历array数组
        int index = 0;
        // 取出 计数数组中的数
        for (int i = 0; i < count.length; i++) {
            while (count[i] > 0) {
                array[index] = i + min;
                index++;
                count[i]--;
            }
        }
    }

}
